//
//  UploadManager.m
//  APIRequest
//
//  Created by 侯伟 on 2019/5/23.
//  Copyright © 2019 侯伟. All rights reserved.
//

#import "UploadManager.h"
#import "HResponse.h"
#import "AFNetworking.h"
#import "YYModel.h"

@interface UploadManager ()

@property (nonatomic, strong) AFHTTPSessionManager *manager;

@property (nonatomic, strong) NSDictionary *headers;

@end

@implementation UploadManager

- (instancetype)init{
    if (self = [super init]){
        [self initManager];
    }
    return self;
}


- (void)initManager{
    
    AFHTTPSessionManager* _manager = [AFHTTPSessionManager manager];
    
    ///config
    NSURLSessionConfiguration* _config = _manager.session.configuration;
    _config.timeoutIntervalForRequest = 120.f;
    _config.timeoutIntervalForResource = 120.f;
    _config.requestCachePolicy  = NSURLRequestReturnCacheDataElseLoad;
    _config.HTTPCookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    
    ///request
    
    AFHTTPRequestSerializer* requestSerializer = [AFHTTPRequestSerializer serializer];
    _manager.requestSerializer = requestSerializer;
    
    ///response
    AFJSONResponseSerializer* responseSerializer = [AFJSONResponseSerializer serializer];
    responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html",@"text/xml",@"image/jpeg",@"image/png", @"application/octet-stream", @"multipart/form-data",@"text/plain",nil];
    _manager.responseSerializer = responseSerializer;
    
    
    ///securityPolicy
    AFSecurityPolicy* securityPolicy = [AFSecurityPolicy defaultPolicy];
    securityPolicy.allowInvalidCertificates = YES;
    securityPolicy.validatesDomainName = NO;
    _manager.securityPolicy = securityPolicy;
    
    self.manager = _manager;
}

#pragma mark -------------------- public --------------------

+ (instancetype)sharedInstance
{
    static UploadManager *instance = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        instance = [[self alloc] init];
    });
    return instance;
}

- (void)setRequestHeader:(NSDictionary *)requestHeader{
    
    _headers = requestHeader;
    
    [self.manager.requestSerializer clearAuthorizationHeader];
    
    if(requestHeader.count){
        [requestHeader enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
            [self.manager.requestSerializer setValue:obj forHTTPHeaderField:key];
        }];
    }
}

- (void)upload:(NSString *)url params:(NSDictionary *)params fileConfigs:(NSArray<UploadFile *> *)fileConfigs callback:(RequestResult)callback{
    
//    [self.manager POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
//        
//        if (fileConfigs){
//            for (int i = 0; i < [fileConfigs count]; i ++)
//            {
//                UploadFile *fileConfig = [fileConfigs objectAtIndex:i];
//                [formData appendPartWithFileData:fileConfig.fileData name:fileConfig.paramName fileName:fileConfig.fileName mimeType:fileConfig.mimeType];
//            }
//        }
//        
//    } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//#ifdef DEBUG
//        NSLog(@"\n Request:%@->response:%@\n", url, responseObject);
//#endif
//        if (callback){
//            callback(responseObject,YES);
//        }
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//#ifdef DEBUG
//        NSLog(@"\n Request:%@->Error:%@\n", url, error.localizedDescription);
//#endif
//        if (callback){
//            callback(error,NO);
//        }
//    }];
}

@end
